/** ***********************************************************************************
* @file         event_task.c
* @brief        事件处理基础管理任务
* @details      主要用来处理和采集设备中的各种事件，硬件状态、数据采集等操作
* @author       杨小珛
* @date         2020/12/23
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/

#include "event_task.h"
#include "mon_task.h"
#include "power_task.h"
#include "log_task.h"
#include "gnss_task.h"
#include "net_task.h"
#include "net_port.h"
#include "port_task.h"
#include "power_task.h"
#include "battery_task.h"
#include "gsen_task.h"
#include "save_task.h"
#include "ble_task.h"
#include "rtc_task.h"
#include "com_lib.h"
#include "qxwz_service.h"
#include "gnss_task.h"
#include "gaoz_api.h"
#include "vehicle_data_process.h"
#include "math.h"

/*-------------------------------------------------------------------------
        全局变量定义(Global variable definition)
-----------------------------------------------------------------------*/
GaozMutexHandle_t event_control_mutex;   //互斥操作
GaozQueueHandle_t event_msg_queue;
event_input_data_t event_input_data;

uint8_t save_data[64] = {0};
uint16_t save_data_len = 0;

static uint8_t  vehcile_warning_st = 0;
static uint32_t power_on_position_flag = 0;
static uint32_t event_timestamp_save_location;
static uint32_t event_system_run_status_time;
static uint32_t power_on_time  = 0;
#if 0
static uint32_t event_sim_card_rest_time = 0;
#endif
static void event_hand_collect_task( void * pvParameters);
static void event_queue_create(void);
static uint8_t event_queue_get(event_trig_msg_s *event_msg);

static void even_kl15_state_detection( void );
static void even_power_state_detection( void );
static void even_gnss_state_detection( void );
static void even_battery_state_detection( void );
static void even_gsen_state_detection( void );
static void even_can_state_detection( void );
static void even_flash_state_detection( void );
static void even_ble_state_detection( void );
static void even_check_wake_up_state( uint32_t res );
static void even_check_wake_up_state( uint32_t res );
static void even_net_anomaly_detection( void );
static void even_sim_state_detection( void );
static void even_vehicle_shifting_detection( void );

static void event_save_system_timestamp_write( uint32_t timestamp );
static void event_save_system_timestamp_read( void );

static void even_net_data_volume_detection( void );
static void even_gnss_sensor_speed_data_detection( void );
static void even_ble_connect_time_detection( void );
static void even_system_run_status_detection( void );

static void even_sim_card_rset_time_read( void );
/** ****************************************************************************
* @remarks       void event_create_task(unsigned portBASE_TYPE uxPriority)
* @brief         创建事件基础管理任务
* @param[in]     uxPriority 任务优先级；
* @param[out]    无
* @return        无
* @attention     系统启动后创建
*******************************************************************************/
void event_create_task(unsigned portBASE_TYPE uxPriority)
{
    event_timestamp_save_location = 0;
    event_system_run_status_time = 0;
    memset((uint8_t *)&event_input_data , 0x00 , sizeof( event_input_data_t ));
    event_queue_create();
    event_control_mutex = gaoz_mutex_create();/*创建事件管理互斥体*/
    if(event_control_mutex == 0)
    {
        while(1);
    }
    event_save_system_timestamp_read();       /*读取存储时间，进行系统时间设置*/
    even_sim_card_rset_time_read();           /*读取SIM卡异常复位次数*/
    gaoz_thread_create(event_hand_collect_task, EVENT_MAN_NAME, 1024*8, NULL, uxPriority);
}

/** ****************************************************************************
* @remarks       static void event_queue_create(void)
* @brief         创建事件采集消息队列
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     消息队列格式 event_trig_msg_s ， 消息队列深度 EVENT_TRIG_MSG_QUEUE_DEPTH
*******************************************************************************/
static void event_queue_create(void)
{
    event_msg_queue = gaoz_queue_create(EVENT_TRIG_MSG_QUEUE_DEPTH, sizeof(event_trig_msg_s));
    if(event_msg_queue == 0)
    {
        while(1);
    }
}
/** ****************************************************************************
* @remarks       static uint8_t event_queue_get(event_trig_msg_s *event_msg)
* @brief         获取事件采集任务消息队列数据
* @param[in]     无
* @param[out]    *event_msg 事件消息结构体数据
* @return        1 有数据 0 没有数据
* @attention     消息一直处于阻塞中，通过1S钟定时器发送消息
*******************************************************************************/
static uint8_t event_queue_get(event_trig_msg_s *event_msg)
{
    uint8_t ret = 0;
    if(event_msg_queue == 0)
    {
        event_queue_create();
    }
    /*一直处于阻塞状态*/
    if( gaoz_queue_get( event_msg_queue,event_msg,EVENT_SAM_REV_RUN_WAIT_TIME ) == 0 )
    {
        ret = 1;
    }
    
    return ret;
}
/** ****************************************************************************
* @remarks       uint8_t event_queue_put(event_trig_msg_s *event_msg)
* @brief         发送事件采集任务消息队列数据
* @param[in]     *event_msg 事件消息结构体数据
* @param[out]    无
* @return        1 发送数据成功 0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t event_queue_put(event_trig_msg_s *event_msg)
{
    uint8_t ret = 0;
    if( event_msg_queue == 0 )
    {
        return ret;
    }
    /*发送事件处理消息队列数据，超时时间10ms*/
    if( gaoz_queue_put(event_msg_queue,event_msg, 10) == 0 )
    {
        ret = 1;
    }
    return ret;
}

/** ****************************************************************************
* @remarks       uint8_t event_queue_put_isr(event_trig_msg_s *event_msg)
* @brief         在中断中发送事件采集任务消息队列数据
* @param[in]     *event_msg 事件消息结构体数据
* @param[out]    无
* @return        1 发送数据成功 0 发送数据失败
* @attention     在中断函数中使用
*******************************************************************************/
uint8_t event_queue_put_isr(event_trig_msg_s *event_msg)
{
    uint8_t ret = 0;
    if( event_msg_queue == 0 )
    {
        return ret;
    }
    /*发送事件处理消息队列数据，超时时间10ms*/
    if( gaoz_queue_put_isr( event_msg_queue,event_msg ) == 0 )
    {
        ret = 1;
    }
    return ret;
}


/** ****************************************************************************
* @remarks       void car_save_data_to_flash(void)
* @brief         设置车辆存储数据
* @param[in]     len 数据长度
* @param[in]     data 设置的数据
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void car_save_data_to_flash(void)
{ 
    if(gaoz_save_vin_data_infor_write((uint8_t*)save_data,save_data_len)== pdPASS) 
    {
        log_debug_printf( DEBUG_MODE,EVENT_MAN_NAME ,"reset save car date ok\r\n");
    }
}

/** ****************************************************************************
* @remarks       void event_input_data_set( event_input_e event , uint32_t data )
* @brief         设置车辆存储数据
* @param[in]     len 数据长度
* @param[in]     data 设置的数据
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void event_input_save_data_set( void *data , uint16_t len )
{
    memcpy(save_data, data, len);
    save_data_len = len;
}

/** ****************************************************************************
* @remarks       void event_input_data_set( event_input_e event , uint32_t data )
* @brief         设置事件数据
* @param[in]     event  设置数据类型
* @param[in]     data 设置的数据
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void event_input_data_set( event_input_e event , uint32_t data )
{
    event_trig_msg_s trig_msg;
    if( event == EVENT_POWER ) 
    {
        event_input_data.power = data;
    }
    else if( event == EVENT_SPEED )
    {
        event_input_data.speed = data;
    }
    else if( event == EVENT_ENGINE )
    {
        event_input_data.engine = data;
        /**为了降低 设备从休眠状态转变为运行状态 的时间，增加信号触发机制***********/
        trig_msg.command_id = EVENT_TRIG_PWR;
        trig_msg.returns_res = 0;
        event_queue_put( (event_trig_msg_s *)&trig_msg );
    }
    else if( event == EVENT_VEHICLE)
    {
        event_input_data.vehicle = data;
    }
    else if( event == EVENT_SIMCARD )
    {
        event_input_data.simcard = data;
    }
    else if( event == EVENT_DIRECTION )
    {
        event_input_data.direction = data;
    }
    else if( event == EVENT_SYSTEMSTATUS )
    {
        event_input_data.systemstatus = data;
    }
}

/** ****************************************************************************
* @remarks       uint32_t event_input_data_get( event_input_e event )
* @brief         获取事件数据
* @param[in]     event  设置数据类型
* @param[out]    无
* @return        返回查询的数据
* @attention     无
*******************************************************************************/
uint32_t event_input_data_get( event_input_e event )
{
    uint32_t data = 0;
    if( event == EVENT_POWER ) 
    {
        data = event_input_data.power;
    }
    else if( event == EVENT_SPEED )
    {
        data = event_input_data.speed;
    }
    else if( event == EVENT_ENGINE )
    {
        data = event_input_data.engine;
    }
    else if( event == EVENT_VEHICLE )
    {
       data = event_input_data.vehicle;     
    }
    else if( event == EVENT_SIMCARD )
    {
        data = event_input_data.simcard;
    }
    else if( event == EVENT_DIRECTION )
    {
        data = event_input_data.direction;
    }
    else if( event == EVENT_SYSTEMSTATUS )
    {
        data = event_input_data.systemstatus;
    }

    return data;
}

/** ****************************************************************************
* @remarks       static void even_kl15_state_detection( void )
* @brief         检测KL15的状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_kl15_state_detection( void )
{   
    #if 0
    if( port_collect_acc_pin_state_get() == 1 )
    {
        sys_switch_state_set( SYS_KL15 ,SYS_KL15_LOW ); 
    }
    else
    {
        sys_switch_state_set( SYS_KL15 ,SYS_KL15_HIGH );
    }
    #endif
    if( event_input_data_get( EVENT_ENGINE ) == 0 )        /*当车辆处于停止状态时，检测车身状态是否为0，不为0时系统启动全速运行*/
    {
        if( event_input_data_get( EVENT_VEHICLE ) == 0 )
        {
            if( sys_switch_state_get( SYS_KL15 ) != SYS_KL15_LOW )
            {
                sys_switch_state_set( SYS_KL15 ,SYS_KL15_LOW ); 
                sys_switch_state_set( SYS_WVEH , SYS_WVEH_STOP);
                log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle KL15 low level,engine status stop,vehicle status stop\r\n",0);
            }
        }
        else
        {
            if( sys_switch_state_get( SYS_KL15 ) != SYS_KL15_HIGH )
            {
                sys_switch_state_set( SYS_KL15 ,SYS_KL15_HIGH );
                sys_switch_state_set( SYS_WVEH , SYS_WVEH_START);
                log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle KL15 high level,engine status stop,vehicle status start\r\n",0);
            }
        }
    }
    else
    {
        event_input_data_set( EVENT_VEHICLE , 0 );      /*当车辆处于运行状态时，车身状态设置为0*/
        if( sys_switch_state_get( SYS_KL15 ) != SYS_KL15_HIGH )
        {
            sys_switch_state_set( SYS_KL15 ,SYS_KL15_HIGH );
            sys_switch_state_set( SYS_WVEH , SYS_WVEH_START);
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle KL15 high level,engine status start\r\n",0);
        }
    }
}

/** ****************************************************************************
* @remarks       static void even_sys_power_state_detection( void )
* @brief         车身电源状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_power_state_detection( void )
{
	uint32_t va = 0;
    uint32_t veh_va = 0;
    veh_va = event_input_data_get( EVENT_POWER );
    va = port_adc_gpio_value_get( GET_ADC0 ) * 11;                      /*计算电源采样的电压值*/
    
	if(( va > SYS_POWER_UP_VALUE ) || ( veh_va > SYS_POWER_UP_VALUE ))  /*外部电压正常*/
	{
        if( sys_hard_state_get( SYS_VEHICLE ) != VEH_POWER_NORMAL )
        {   
    		sys_hard_state_set( SYS_POWER , SYS_POWER_ON );              /*车辆电源打开*/
    		sys_hard_state_set( SYS_VEHICLE , VEH_POWER_NORMAL );        /*车辆电压正常*/
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle power supply normal\r\n",0);
        }
	}
	else
	{
	    if( sys_hard_state_get( SYS_VEHICLE ) != VEH_POWER_UNDER )
        {
            sys_hard_state_set( SYS_VEHICLE , VEH_POWER_UNDER );            /*车辆电压异常*/
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle power under voltage\r\n",0);
        }
		if(( va < SYS_POWER_DOWN_VALUE)&&( veh_va < SYS_POWER_DOWN_VALUE ))/*车辆处于掉电状态*/
		{
		    if(sys_hard_state_get( SYS_POWER ) != SYS_POWER_OFF )
            {      
			    sys_hard_state_set( SYS_POWER , SYS_POWER_OFF );          /*车辆电源断开*/
                log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Vehicle power cut off\r\n",0);
            }
		}
	}
}

/** ****************************************************************************
* @remarks       static void even_battery_state_detection( void )
* @brief         检测备用电池状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_battery_state_detection( void )
{
    battery_insta_state_e charg_st = BAT_INSTA_INIT;
    charg_st = battery_insta_state_get();
    if( charg_st == BAT_INSTA_NO )
    {
        if( sys_hard_state_get( SYS_BAT )   != SYS_BAT_FAULE )
        {   
            sys_hard_state_set( SYS_BAT , SYS_BAT_FAULE ); 
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Backup battery failure\r\n",0);
        }
    }
    else if( charg_st == BAT_INSTA_YES )
    {
        if( sys_hard_state_get( SYS_BAT ) !=   SYS_BAT_NORMAL )
        {
            sys_hard_state_set( SYS_BAT , SYS_BAT_NORMAL );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Backup battery normal\r\n",0);
        }
    }
    else
    {
        sys_hard_state_set( SYS_BAT , SYS_BAT_INIT );
    }   
}


/******************************************************************************
* @remarks       uint32_t reset_reason_set(uint8_t reason)
* @brief         设置设备的重启原因
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint32_t reset_reason_set(uint8_t reason)
{
    uint32_t ret = pdFAIL;
    uint8_t reasonbuf[2];

    gaoz_log_debug_printf(INFOR_MODE,"800b", "reset_reason_set[%d]\r\n",reason);


    if((reason >= EVENT_RESET_REASON_MIN_VALUE) && (reason <= EVENT_RESET_REASON_MAX_VALUE))
    {
        reasonbuf[0] = reason;
        reasonbuf[1] = ~(reason);

        ret = save_reset_reason_write(reasonbuf,2);

        gaoz_log_debug_printf(INFOR_MODE,"800b", "reset_reason_set[%d] ret:%d \r\n",reason,ret);
    }
 
    return ret;
}

/******************************************************************************
* @remarks       uint32_t reset_reason_get(uint8_t* reason)
* @brief         获取设备的重启原因
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint32_t reset_reason_get(uint8_t* reason)
{
    uint32_t ret = pdFAIL;
    uint8_t reasonbuf[2] = {0,0};
    uint8_t xor = 0;

    *reason = 0;//未设置重启标识时的异常重启

    save_reset_reason_read(reasonbuf,2);

    xor = (uint8_t)(~(reasonbuf[1]));

    if(reasonbuf[0] == xor)
    {
        if((reasonbuf[0] >= EVENT_RESET_REASON_MIN_VALUE) && (reasonbuf[0] <= EVENT_RESET_REASON_MAX_VALUE))
        {
            ret = pdPASS;
            *reason = reasonbuf[0];

            gaoz_log_debug_printf(INFOR_MODE,"800b", "reset_reason_get[%d]\r\n",reasonbuf[0]);
        } 
    }
    
    if(ret != pdPASS) 
    {
        gaoz_log_debug_printf(INFOR_MODE,"800b", "reset_reason_get fail [%d] [%d]\r\n",reasonbuf[0],reasonbuf[1]);
    }

    return ret;
}

/******************************************************************************
* @remarks      uint32_t reset_reason_clear(void)
* @brief         鉴权成功，清除重启的结果
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint32_t reset_reason_clear(void)
{
    uint32_t ret = pdFAIL;
    uint8_t reasonbuf[2] =  {0x00,0x00};
 
    ret = save_reset_reason_write(reasonbuf,2); 
 
    return ret;
}





/** ****************************************************************************
* @remarks       void event_save_system_timestamp_write( uint32_t timestamp )
* @brief         保存系统时间戳
* @param[in]     timestamp 时间戳
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void event_save_system_timestamp_write( uint32_t timestamp )
{
    uint32_t timestamp_addr = SF_TIMESTAMP_DATA_ADDR, ret = pdFAIL;
    
    if( event_timestamp_save_location == 0 )
    {
        sfb_block_reset( SF_TIMESTAMP_DATA_ADDR, SF_TIMESTAMP_DATA_FlAG_SECTORS );
    }
    timestamp_addr += event_timestamp_save_location * sizeof(uint32_t);
    if( timestamp_addr >= ( SF_TIMESTAMP_DATA_ADDR + SF_TIMESTAMP_DATA_SIZE ) )
    {
        timestamp_addr = SF_TIMESTAMP_DATA_ADDR;
    }
    if( sfb_write_buf( timestamp_addr, ( uint8_t *)&timestamp, sizeof(uint32_t) ) == pdPASS )
    {
        ret = pdPASS;
    }
    if( ret == pdPASS )
    {
        event_timestamp_save_location ++;
        if( event_timestamp_save_location >= ( SF_TIMESTAMP_DATA_SIZE / sizeof(uint32_t) ) )
        {
            event_timestamp_save_location = 0;
        }
        log_debug_printf( DEBUG_MODE,EVENT_MAN_NAME ,"Write flash system time, timestamp = %ld,location = %d\r\n", timestamp, event_timestamp_save_location );
    }
    else
    {
        event_timestamp_save_location = 0;
    }
}

/** ****************************************************************************
* @remarks       void event_save_system_timestamp_read( void )
* @brief         读取系统时间戳
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void event_save_system_timestamp_read( void )
{
    #define SYSTEM_INIT_MIN_TIMESTAMP ( 1577808000 ) //2020-01-01 00:00:00
    #define SYSTEM_INIT_MAX_TIMESTAMP ( 1893427200 ) //2030-01-01 00:00:00
    #define SYSTEM_TIMESTAMP_BUF_LEN  ( SF_TIMESTAMP_DATA_SIZE / sizeof( uint32_t ) )

    uint32_t *timestamp_p = NULL;
    uint32_t timestamp_max = 0, num = 0;
    uint8_t rtc_string[64] = { 0 };
    
    timestamp_p = (uint32_t *)gaoz_fibo_malloc( SF_TIMESTAMP_DATA_SIZE );
    if( timestamp_p != NULL )
    {
        if( sfb_read_buf( SF_TIMESTAMP_DATA_ADDR, (uint8_t *)timestamp_p,  SF_TIMESTAMP_DATA_SIZE ) == pdPASS )
        {
            /*将FLASH 中的数据全部读出，筛选出最大的数据。进行系统时间设置*/
            for( num = 0; num < SYSTEM_TIMESTAMP_BUF_LEN; num ++ )
            {
                if( ( timestamp_p[num] >= SYSTEM_INIT_MIN_TIMESTAMP ) && ( timestamp_p[num] <= SYSTEM_INIT_MAX_TIMESTAMP ))
                {    
                    event_timestamp_save_location ++;
                    if( event_timestamp_save_location == SYSTEM_TIMESTAMP_BUF_LEN )
                    {
                        event_timestamp_save_location = 0;
                    }
                    timestamp_max = timestamp_p[num];
                }   
                else
                {
                    break;
                }
            }
            /*查询到正确时间，进行系统时间设置*/
            if( ( timestamp_max >= SYSTEM_INIT_MIN_TIMESTAMP ) && ( timestamp_max <= SYSTEM_INIT_MAX_TIMESTAMP ))
            {
                rtc_sys_set( RTC_TIM_TIMESTAMP, (uint32_t *)&timestamp_max );
                log_debug_printf( DEBUG_MODE,EVENT_MAN_NAME ,"Read flash system time, timestamp = %ld,location = %d\r\n", timestamp_max, event_timestamp_save_location );
                rtc_sys_get( RTC_TIM_STRING, (uint8_t *)rtc_string );
                log_debug_printf( DEBUG_MODE,EVENT_MAN_NAME ,"Read system time = %s\r\n", rtc_string );
            }
        }
        gaoz_fibo_free((void *)timestamp_p);
    }
}

/** ****************************************************************************
* @remarks       static void even_rtc_state_detection( void )
* @brief         检测RTC系统时钟的状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_rtc_state_detection( void )
{
    #define SYS_TIMESTAMP_STORED_TIME_PERIOD ( 60 * GAOZ_TICK_PERIOD_S )  /*时间戳存储的周期*/
    static uint32_t sys_timestamp_time_tick = 0;
    uint8_t net_st = 0;
	uint32_t gps_s = 0;
    uint32_t timestamp = 0;

	gps_s = gnss_status_get();
    net_module_run_state_get( MOD_STATE ,(uint8_t *)&net_st );
    /*网络状态正常，GPS定位状态正常都可以对时钟进行设置*/
    if( net_st == MODULE_NET_OPEN ) 
    {   
        if( sys_hard_state_get( SYS_RTC ) != SYS_RTC_OK )
        {   
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"System time network updated successfully\r\n",0);
            sys_hard_state_set( SYS_RTC , SYS_RTC_OK );
            sys_timestamp_time_tick = gaoz_get_systick(); 
        }
    }
    else
    {
        if( gps_s == GNSS_POS_OK )
        {
            if( sys_hard_state_get( SYS_RTC ) != SYS_RTC_OK )
            {
                gnss_sys_time_set();
                log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"System time GPS set successfully\r\n",0);
                sys_hard_state_set( SYS_RTC , SYS_RTC_OK );
                sys_timestamp_time_tick = gaoz_get_systick(); 
            }
        }
    }
    /*当时间准确时，对时钟数据进行实时存储，防止系统复位启动后使用最新的时钟数据进行系统时钟的回复*/
    if( sys_hard_state_get( SYS_RTC ) == SYS_RTC_OK )   
    {
        if( pdPASS == sys_timestamp_delay(( uint32_t * )&sys_timestamp_time_tick, SYS_TIMESTAMP_STORED_TIME_PERIOD ))
        {
            rtc_sys_get( RTC_TIM_TIMESTAMP, (uint32_t *)&timestamp );
            event_save_system_timestamp_write( timestamp );
        }
    }
}

/** ****************************************************************************
* @remarks       static void even_gps_state_detection( void )
* @brief         检测GNSS状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_gnss_state_detection( void )
{
	uint32_t gps_s = 0;
	gps_s = gnss_status_get();

    if( gps_s == GPS_MODEL_OK )        // GPS模块正常
    {
        if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_NORMAL )
        {
		    sys_hard_state_set( SYS_GPS , SYS_GPS_NORMAL );  
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module normal\r\n",0);
        }
    }
    else if( gps_s == GPS_MODEL_ERROR )     // GPS模块故障
    {
        if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_FAULE )
        {
            sys_hard_state_set( SYS_GPS , SYS_GPS_FAULE );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module failure\r\n",0);
        }
    }   
    else if( gps_s == GPS_MODEL_DATA_ERROR )// GPS模块数据异常
    {
        if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_UART_ERR )
        {
            sys_hard_state_set( SYS_GPS , SYS_GPS_UART_ERR );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module uart failure\r\n",0);
        }
    }
    else if( gps_s == GPS_MODEL_POSI_ERROR )// GPS模块定位异常
    {
        if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_POS_ERR )
        {   
		    sys_hard_state_set( SYS_GPS , SYS_GPS_POS_ERR );  
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module position failure\r\n",0);
        }
    }
    else if( gps_s == GPS_MODEL_POSI_OK )
    {
        if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_POS_OK )
        {   
		    sys_hard_state_set( SYS_GPS , SYS_GPS_POS_OK );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module positioning normal\r\n",0);
        }
    }
    else
    {
        sys_hard_state_set( SYS_GPS , SYS_GPS_INIT );
    }

#if 0
	if( gps_s == GNSS_POS_OK )              /*模块已经定位*/   
	{	
	    if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_POS_OK )
        {   
		    sys_hard_state_set( SYS_GPS , SYS_GPS_POS_OK );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module positioning normal\r\n",0);
        }
	}
	else if( gps_s == GNSS_MOD_OK )        /*模块正常工作*/
	{	
	    if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_NORMAL )
        {   
		    sys_hard_state_set( SYS_GPS , SYS_GPS_NORMAL );  
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module normal\r\n",0);
        }
	}
	else if( gps_s == GNSS_MON_ERR )       /*模块异常*/
	{	
	    if( sys_hard_state_get( SYS_GPS ) != SYS_GPS_FAULE )
        {
            sys_hard_state_set( SYS_GPS , SYS_GPS_FAULE );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"GNSS module failure\r\n",0);
        }
	}
	else    /*模块状态暂时未确定*/
	{	
		sys_hard_state_set( SYS_GPS , SYS_GPS_INIT );
	}
#endif
}

/** ****************************************************************************
* @remarks       static void even_net_state_detection( void )
* @brief         检测网络状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_net_state_detection( void )
{
    uint8_t net_st = 0;
    net_module_run_state_get( MOD_STATE ,(uint8_t *)&net_st );
    if( net_st == MODULE_NET_ERR )   /* 网络异常 */
    {
        if( sys_hard_state_get( SYS_GPRS ) != SYS_GPRS_NET_ERR )
        {
            sys_hard_state_set( SYS_GPRS , SYS_GPRS_NET_ERR ); 
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Network drive failure\r\n",0);
        }
    }
    else if( net_st == MODULE_NET_OPEN )  /* 模块网络正常*/
    {   
        if( sys_hard_state_get( SYS_GPRS ) !=   SYS_GPRS_NET_OK )
        {
            sys_hard_state_set( SYS_GPRS , SYS_GPRS_NET_OK ); 
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Registration network normal\r\n",0);
        }
    }
    else if( net_st == MODULE_NET_RST )   /* 网络需要复位*/
    {
        if( sys_hard_state_get( SYS_GPRS )   != SYS_GPRS_FAULE )
        {   
    		sys_hard_state_set( SYS_GPRS , SYS_GPRS_FAULE ); 
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Network registration failed\r\n",0);
        }
    }
    else
    {
        sys_hard_state_set( SYS_GPRS , SYS_GPRS_INIT ); 
    }
}

/** ****************************************************************************
* @remarks       static void even_sim_card_rset_time_read( void )
* @brief         读取SIM卡异常复位次数
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_sim_card_rset_time_read( void )
{
#if 0    
    #define SIM_CARD_RESET_MASK     ( 0x53494D00 )  /*SIM卡异常复位标志*/
    sfb_read_buf( SF_SIM_CARD_DATA_ADDR, (uint8_t *)&event_sim_card_rest_time,  sizeof( uint32_t) );
    if(( event_sim_card_rest_time & 0xFFFFFF00 ) != SIM_CARD_RESET_MASK)
    {
        event_sim_card_rest_time = SIM_CARD_RESET_MASK;
        sfb_block_reset( SF_SIM_CARD_DATA_ADDR, SF_SIM_CARD_DATA_FlAG_SECTORS );
        sfb_write_buf( SF_SIM_CARD_DATA_ADDR, ( uint8_t *)&event_sim_card_rest_time, sizeof(uint32_t) );
    }
#endif
}
/** ****************************************************************************
* @remarks       static void even_sim_state_detection( void )
* @brief         检测SIM卡状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_sim_state_detection( void )
{
#if 0
    #define SIM_CARD_RESET_MASK     ( 0x53494D00 )  /*SIM卡异常复位标志*/
    #define SIM_CARD_RESET_TIME     ( 0x00000005 )  /*SIM卡异常时复位5次*/
    #define SIM_ANOMALY_MAX_TIME    ( 10 )
    static uint32_t sim_anomaly_time = 0;
#endif    
    uint8_t sim_st = 0;
    net_module_run_state_get( SIM_STATE , (uint8_t *)&sim_st );
    if( sim_st == 1 )   /* SIM卡正常 */
    {
        if( sys_hard_state_get( SYS_SIM ) != SYS_SIM_NORMAL )
        {
            sys_hard_state_set( SYS_SIM , SYS_SIM_NORMAL ); 
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"SIM card normal\r\n",0);
        }
    }
    else if( sim_st == 0 ) /*SIM卡异常*/
    {
        if( sys_hard_state_get( SYS_SIM ) != SYS_SIM_FAULE )
        {
            sys_hard_state_set( SYS_SIM , SYS_SIM_FAULE );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"SIM card failed\r\n",0);
        }
    }
#if 0
    /*当系统启动持续10S检测SIM卡异常后对系统进行复位，连续复位5次*/
    if( sys_hard_state_get( SYS_SIM ) == SYS_SIM_FAULE )
    {
        sim_anomaly_time++;
        if(( sim_anomaly_time >= SIM_ANOMALY_MAX_TIME ) && (( event_sim_card_rest_time & 0x000000FF ) < SIM_CARD_RESET_TIME ))
        {
            sim_anomaly_time = 0;
            if(( event_sim_card_rest_time & SIM_CARD_RESET_MASK ) == SIM_CARD_RESET_MASK )
            {
                event_sim_card_rest_time = event_sim_card_rest_time + 1;
            }
            else
            {
                event_sim_card_rest_time = SIM_CARD_RESET_MASK + 1;
            }
            sfb_block_reset( SF_SIM_CARD_DATA_ADDR, SF_SIM_CARD_DATA_FlAG_SECTORS );
            sfb_write_buf( SF_SIM_CARD_DATA_ADDR, ( uint8_t *)&event_sim_card_rest_time, sizeof(uint32_t) );
            if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
            {   
                log_debug_printf(ERROR_MODE,EVENT_MAN_NAME ,"[%s]SIM card abnormal time timeout, system reset\r\n",sys_err_infor_pointer_get( 22 ));
                sys_abnormal_reset_set( SYS_RESET_NET );    /*设置系统网络运行异常复位操作*/
            }
        }
    } 
    else if( sys_hard_state_get( SYS_SIM ) == SYS_SIM_NORMAL  )
    {
        if(( event_sim_card_rest_time & 0x000000FF ) == SIM_CARD_RESET_TIME )
        {
            event_sim_card_rest_time = SIM_CARD_RESET_MASK;
            sfb_block_reset( SF_SIM_CARD_DATA_ADDR, SF_SIM_CARD_DATA_FlAG_SECTORS );
            sfb_write_buf( SF_SIM_CARD_DATA_ADDR, ( uint8_t *)&event_sim_card_rest_time, sizeof(uint32_t) );
        }
        sim_anomaly_time = 0;
    }
    else
    {   
        sim_anomaly_time = 0;
    }
#endif
}
/** ****************************************************************************
* @remarks       static void even_gsen_state_detection( void )
* @brief         加速度传感器状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_gsen_state_detection( void )
{
    #define GSEN_WORK_FLAULE_RESET ( 1 )
    gsen_bmi160_work_state_e st;
    static uint8_t gsen_work_reset_state = 0;

    st = gsen_sensor_work_st_get(); 
    if( st == GSEN_WORK_ST_ERR )            /* 加速度传感器异常 */
    {
        if( sys_hard_state_get( SYS_GSEN ) != SYS_GSEN_FAULE )
        {
            sys_hard_state_set( SYS_GSEN , SYS_GSEN_FAULE );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Acceleration sensor failure\r\n",0);
            if( sys_abnormal_reset_get() == SYS_RESET_INIT )
            {   
                gsen_work_reset_state = GSEN_WORK_FLAULE_RESET;
                reset_reason_set(EVENT_RESET_REASON_GSENSOR_ERR);
                sys_abnormal_reset_set( SYS_RESET_RUN );    /*当加速度芯片出现异常时，由于再次初始化不能恢复正常，需要在系统进入休眠时进行系统复位*/
            }
        }
    }
    else if( st == GSEN_WORK_ST_OK )        /* 加速度传感器正常 */
    {
        if( sys_hard_state_get( SYS_GSEN ) != SYS_GSEN_NORMAL )
        {
            sys_hard_state_set( SYS_GSEN , SYS_GSEN_NORMAL );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Acceleration sensor normal\r\n",0);
            if( gsen_work_reset_state == GSEN_WORK_FLAULE_RESET )
            {
                gsen_work_reset_state = 0;
                
                sys_abnormal_reset_set( SYS_RESET_INIT );   /*GSEN硬件恢复正常时，对复位标识进行初始化操作*/
            }
        }
    }
    else
    {
        sys_hard_state_set( SYS_GSEN , SYS_GSEN_INIT );
    }
}

/** ****************************************************************************
* @remarks       static void even_gsen_state_detection( void )
* @brief         CAN网络状态的检查，其实检测的是MCU的状态
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_can_state_detection( void )
{   
    sys_hard_state_set( SYS_CAN , SYS_CAN_INIT ); 
}

/** ****************************************************************************
* @remarks       static void even_flash_state_detection( void )
* @brief         FLASH 状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_flash_state_detection( void )
{
    save_data_flash_state_e st;
    st = save_data_flash_state_get();
    if( st == SAVE_DATA_FLASH_ERR )
    {
        if( sys_hard_state_get( SYS_FLASH ) != SYS_FLASH_FAULE )
        {
            sys_hard_state_set( SYS_FLASH , SYS_FLASH_FAULE );   /* FLASH异常 */
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Flash hardware failure\r\n",0);
        }
    }
    else if( st == SAVE_DATA_FLASH_OK )
    {
        if( sys_hard_state_get( SYS_FLASH ) != SYS_FLASH_NORMAL )
        {
            sys_hard_state_set( SYS_FLASH , SYS_FLASH_NORMAL );/* FLASH正常 */
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Flash hardware normal\r\n",0);
        }
    }
}

/** ****************************************************************************
* @remarks       static void even_ble_state_detection( void )
* @brief         FLASH 状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_ble_state_detection( void )
{
    ble_gatt_connect_e st;
    st = ble_drive_gatt_connect_get();
    if( st == BLE_GATT_INIT )
    {   
        sys_hard_state_set( SYS_BLE , SYS_BLE_INIT );
    }
    else if( st == BLE_GATT_ERROR )
    {
        if( sys_hard_state_get( SYS_BLE ) != SYS_BLE_FAULE )
        {
            sys_hard_state_set( SYS_BLE , SYS_BLE_FAULE );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Bluetooth module failure\r\n",0);
        }
    }
    else 
    {
        if( sys_hard_state_get( SYS_BLE ) != SYS_BLE_NORMAL )
        {
            sys_hard_state_set( SYS_BLE , SYS_BLE_NORMAL );
            log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Bluetooth module normal\r\n",0);
        }
    }
}

/** ****************************************************************************
* @remarks       static void even_net_anomaly_detection( void )
* @brief         网络运行异常检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     当检测连续10分钟异常后，进行当前的车速进行判断。如果车速小于10kM/h时进行系统复位
*******************************************************************************/
static void even_net_anomaly_detection( void )
{
    static uint32_t net_anomaly_time = 0;
    static uint32_t sim_anomaly_time  = 0;
    static uint32_t net_normal_system_time = 0;
    uint32_t net_err_time =  0;
    uint32_t cur_time = 0;
    uint32_t gnss_speed = 0;
    uint32_t veh_speed = 0;
    uint8_t net_st = 0;
    net_module_run_state_get( MOD_STATE ,(uint8_t *)&net_st );
    if( net_st != MODULE_NET_OPEN )                          /*检测到当前网络异常*/
    {
        if(power_on_time<600)//debug mask
        {
            return ;//上电5min内不重启
        }
 
        cur_time = osiEpochSecond(); 

        if(net_normal_system_time == 0)
        {
            net_normal_system_time = osiEpochSecond();
        }

        net_err_time = (cur_time >net_normal_system_time)?cur_time-net_normal_system_time:net_normal_system_time-cur_time;

        log_debug_printf(DEBUG_MODE,EVENT_MAN_NAME ,"network not register reboot time:%d/%d\r\n",net_err_time,EVENT_NET_ANOMALY_MAX_TIME);
        if( net_err_time > EVENT_NET_ANOMALY_MAX_TIME )
        { 
            gnss_speed_get( &gnss_speed );                    /*获取当前GNSS的速度*/
            veh_speed = event_input_data_get( EVENT_SPEED );/*获取当前车辆的速度*/
            /*判断当前的车辆速度和GNSS速度是否小于预设值，如果条件成立则复位系统*/
            if(( gnss_speed < EVENT_NET_ANOMALY_GNSS_SPEED ) && ( veh_speed < EVENT_NET_ANOMALY_VEH_SPEED ))
            {
                sim_anomaly_time ++;
                if( event_input_data_get(   EVENT_SIMCARD ) == 0 )
                {
                    event_input_data_set(   EVENT_SIMCARD , 1 );
                    log_debug_printf(DEBUG_MODE,EVENT_MAN_NAME ,"Network abnormal, Notify APP to cut card\r\n");
                }
                else if(( event_input_data_get(   EVENT_SIMCARD ) == 2 ) || ( sim_anomaly_time >= EVENT_SIM_ANOMALY_MAX_TIME ))
                {
                    sim_anomaly_time = EVENT_SIM_ANOMALY_MAX_TIME;
                    if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
                    {   
                        log_debug_printf(ERROR_MODE,EVENT_MAN_NAME ,"[%s]Network abnormal time timeout, system reset\r\n",sys_err_infor_pointer_get( 16 ));
                        reset_reason_set(EVENT_RESET_REASON_NET_NOT_REG);
                        sys_abnormal_reset_set( SYS_RESET_NET );    /*设置系统网络运行异常复位操作*/
                    }
                }
            }
        }
    }
    else
    {
        net_normal_system_time = osiEpochSecond();
        net_anomaly_time = 0;
        sim_anomaly_time = 0;
    }
}

/** ****************************************************************************
* @remarks       static void even_vehicle_shifting_detection( void )
* @brief         检测车身移位
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     当检测到车身处于移位唤醒时，打开GPS 检测运行距离。连续检测10分钟。
*******************************************************************************/
static void even_vehicle_shifting_detection( void )
{
    static uint32_t veh_shifting_time = 0;
    static uint32_t gnss_mil_now = 0 , gnss_mil_old = 0;
    uint32_t vehicle_st = 0;
    vehicle_st = event_input_data_get( EVENT_VEHICLE );
    if( vehicle_st != 0 )
    {
        veh_shifting_time++;
        if( vehicle_st == 1 )       /*GSEN 移位唤醒开启GNSS重启，系统全速运行*/
        {
            if( veh_shifting_time > EVENT_VEH_SHIFITING_MAX_TIME )
            {
                veh_shifting_time = EVENT_VEH_SHIFITING_MAX_TIME;
                event_input_data_set( EVENT_VEHICLE , 0 );
            }
            else
            {
                gnss_mil_now = gnss_mileage_get();
                if( gnss_mil_now > gnss_mil_old )
                {
                    if(( gnss_mil_now - gnss_mil_old ) >= 100 )
                    {
                        gnss_mil_old = gnss_mil_now;
                        veh_shifting_time = 0;
                    }
                }
            }
        }
        else if( vehicle_st == 2 ) /*网络设置GNSS 重启定位，系统全速运行*/
        {
            if( gnss_static_filter_enable_state_get() != GNSS_SF_ENABLE )
            {
                gnss_static_filter_enable_state_set( GNSS_SF_ENABLE );
            }
            if( veh_shifting_time > EVENT_VEH_GNSS_RESET_MAX_TIME )
            {
                veh_shifting_time = EVENT_VEH_GNSS_RESET_MAX_TIME;
                event_input_data_set( EVENT_VEHICLE , 0 );
            }
        }
    }
    else
    {
        gnss_mil_old = gnss_mileage_get();
        veh_shifting_time = 0;
    }
}

/** ****************************************************************************
* @remarks       static void even_net_data_volume_detection( void )
* @brief         网络数据量检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     实时检测网络传输的数据是否发生变化，10分钟内没有检测到网络数据发送变化需要重启网络；
*******************************************************************************/
static void even_net_data_volume_detection( void )
{
    static uint32_t net_discon_time = 0,net_not_register_time = 0;
    static uint32_t met_data_time[MAX_SOCKET_CONN_NUM] = { 0 };
    static uint32_t send_data_count[MAX_SOCKET_CONN_NUM] = { 0 }, recv_data_count[MAX_SOCKET_CONN_NUM] ={ 0 };
    uint32_t gnss_speed = 0,veh_speed = 0;
    uint8_t index = 0;

    uint8_t net_st = 0;
    net_module_run_state_get( MOD_STATE ,(uint8_t *)&net_st );
    if( net_st != MODULE_NET_OPEN )                          /*异常情况不检测数据 及 连接*/
    {
        net_not_register_time++;
        log_debug_printf(DEBUG_MODE,EVENT_MAN_NAME ,"network not register :%d \r\n", net_not_register_time);

        return;
    }

    net_not_register_time = 0;

    for( index = 0; index < MAX_SOCKET_CONN_NUM; index++ )
    {
        if( net_connection_status_get( index ) == pdPASS )
        {
            net_discon_time = 0;
            /*判断网络接收数据包总量是否发生变化，如果持续10分钟没有产生数据量变化时需要对系统进行复位*/
            if( ( recv_data_count[index] != net_recv_data_total_count_get( index ) ) || ( send_data_count[index] != net_send_data_total_count_get( index ) ) )
            {
                send_data_count[index] = net_send_data_total_count_get( index );
                recv_data_count[index] = net_recv_data_total_count_get( index );
                met_data_time[index] = 0;
                log_debug_printf(DEBUG_MODE,EVENT_MAN_NAME ,"Total Network data:Index = %d,Send = %ld,Recv = %ld\r\n", index, send_data_count[index], recv_data_count[index]);
            }
            else
            {
                met_data_time[index]++;
                if( met_data_time[index] > EVENT_NET_DATA_VOLUME_MAX_TIME )
                {
                    met_data_time[index] = EVENT_NET_DATA_VOLUME_MAX_TIME;
                    gnss_speed_get( &gnss_speed );                    /*获取当前GNSS的速度*/
                    veh_speed = event_input_data_get( EVENT_SPEED );/*获取当前车辆的速度*/
                    /*判断当前的车辆速度和GNSS速度是否小于预设值，如果条件成立则复位系统*/
                    if(( gnss_speed < EVENT_NET_ANOMALY_GNSS_SPEED ) && ( veh_speed < EVENT_NET_ANOMALY_VEH_SPEED ))
                    {
                        if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
                        {
                            log_debug_printf(ERROR_MODE,EVENT_MAN_NAME ,"[%s]Network data volume abnormal time timeout, system reset\r\n",sys_err_infor_pointer_get( 16 ));
                            reset_reason_set(EVENT_RESET_REASON_CON_NOT_DATA);
                            sys_abnormal_reset_set( SYS_RESET_NET );    /*设置系统网络运行异常复位操作*/
                        }
                    }
                }
            }
        }
        else
        {
            net_discon_time++;
            if( net_discon_time >= EVENT_NET_NOT_CON_VOLUME_MAX_TIME )
            {  
                net_discon_time = EVENT_NET_NOT_CON_VOLUME_MAX_TIME;
                gnss_speed_get( &gnss_speed );                    /*获取当前GNSS的速度*/
                veh_speed = event_input_data_get( EVENT_SPEED );/*获取当前车辆的速度*/
                /*判断当前的车辆速度和GNSS速度是否小于预设值，如果条件成立则复位系统*/
                if(( gnss_speed < EVENT_NET_ANOMALY_GNSS_SPEED ) && ( veh_speed < EVENT_NET_ANOMALY_VEH_SPEED ))
                {
                    if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
                    {
                        log_debug_printf(ERROR_MODE,EVENT_MAN_NAME ,"[%s]Network data volume abnormal time timeout, system reset\r\n",sys_err_infor_pointer_get( 16 ));
                        reset_reason_set(EVENT_RESET_REASON_REG_NOT_CON);
                        sys_abnormal_reset_set( SYS_RESET_NET );    /*设置系统网络运行异常复位操作*/
                    }
                }
            }
        }
    }
}

/** ****************************************************************************
* @remarks       static void even_gnss_sensor_speed_data_detection( void )
* @brief         GNSS 速度检测 设置
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_gnss_sensor_speed_data_detection( void )
{   
#if( GNSS_QXWZ == 1 )
    uint32_t vehicle_speed = 0;
    float sensor_speed = 0.0;
    /*GNSS 模块工作正常或者已定位，开始灌入速度*/
	if(( gnss_status_get() == GNSS_POS_OK ) || ( gnss_status_get() == GNSS_MOD_OK ))  
	{	
        vehicle_speed = event_input_data_get( EVENT_SPEED );        /*获取车辆速度,单位KM/H*/
        if( event_input_data_get( EVENT_DIRECTION ) == 1 )          /*车辆前进*/
        {
            sensor_speed = vehicle_speed / 3.6;                     /*将KM/H->M/S*/
        }
        else if( event_input_data_get( EVENT_DIRECTION ) == 2 )     /*车辆后退*/
        {
            sensor_speed = 0 - (vehicle_speed / 3.6);               /*将KM/H->M/S*/
        } 
        else //if( event_input_data_get( EVENT_DIRECTION ) == 0 )   /*停车*/
        {
            sensor_speed = 0;
        }
        if( qxwz_inject_sensor_data_set( sensor_speed , 0 ) != pdPASS )
        {
            //log_debug_printf(INFOR_MODE,EVENT_MAN_NAME ,"QXWZ inject sensor data error,Current speed = %f\r\n", sensor_speed );
        }
	}
#endif
}

/** ****************************************************************************
* @remarks       static void even_ble_connect_time_detection( void )
* @brief         检测BLE 连接时间
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_ble_connect_time_detection( void )
{
    static uint32_t ble_connect_time = 0;
    static uint32_t ble_send = 0, ble_recv = 0;
    /*检测BLE服务连接的时间，超时EVENT_BLE_CONNECT_MAX_TIME后主动断开服务连接*/
    if( ble_drive_gatt_connect_get() == BLE_GATT_CONNECT )
    {
        if(( ble_send != ble_send_data_total_count_get() ) || ( ble_recv != ble_recv_data_total_count_get() ))
        {
            ble_send = ble_send_data_total_count_get();
            ble_recv = ble_recv_data_total_count_get();
            ble_connect_time = 0;
            log_debug_printf(DEBUG_MODE,EVENT_MAN_NAME ,"Total BLE data:Send = %ld,Recv = %ld\r\n", ble_send, ble_recv);
        }
        else
        {
            ble_connect_time++;
            if( ble_connect_time >= EVENT_BLE_CONNECT_MAX_TIME )
            {
                ble_connect_time = EVENT_BLE_CONNECT_MAX_TIME;
                ble_drive_gatt_connect_set( BLE_GATT_OPEN );       /*重新启动BLE服务*/
                log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"BLE service connection times out,automatically disconnected\r\n",0);
            }
        }
    }
    else
    {
        ble_connect_time = 0;
    }
}

/** ****************************************************************************
* @remarks       static void even_system_run_status_detection( void )
* @brief         系统运行状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_system_run_status_detection( void )
{
    event_system_status_s sys_run_st;
    system_work_mode_s sys_work_status = SYS_ST_INIT;
    /*系统检测到发动机状态停止时系统进入休眠状态，如果在10分内不进入休眠模式则任务系统异常需要进行复位*/
    if(( event_input_data_get( EVENT_ENGINE ) == 0 ) && ( event_input_data_get( EVENT_VEHICLE ) == 0 ))
    {
        event_system_run_status_time++; 
        
        if( event_system_run_status_time >= EVENT_SYS_RUN_STATUS_TIME )
        {
            event_system_run_status_time = EVENT_SYS_RUN_STATUS_TIME;  
            if(( sys_abnormal_reset_get() == SYS_RESET_INIT ) || ( sys_abnormal_reset_get() == SYS_RESET_RUN ))
            {   
                reset_reason_set(EVENT_RESET_REASON_NO_SLEEP);
                log_debug_printf(ERROR_MODE,EVENT_MAN_NAME ,"[%s]System sleep run status error, system reset\r\n",sys_err_infor_pointer_get( 22 ));
                sys_abnormal_reset_set( SYS_RESET_SYS );    /*设置系统状态运行异常复位操作*/
            }
        }
    }
    else
    {
        event_system_run_status_time = 0;
    }

    /*设置系统运行的状态*******************************************************/
    sys_run_st.status = 0;
    /*1、设置发动机运行的状态 bit0：1-运行 0-未运行*/
    if( event_input_data_get( EVENT_ENGINE ) == 0 )
    {
        sys_run_st.engine = 0;
    }
    else
    {
        sys_run_st.engine = 1;
    }
    //2、设置车机运行的状态 bit1~bit2：0 - 未设置 1 - 位移设置 2- GPS控制设置*/
    if( event_input_data_get( EVENT_VEHICLE ) == 0 )
    {
        sys_run_st.vehicle = 0;
    }
    else if( event_input_data_get( EVENT_VEHICLE ) == 1 )
    {
        sys_run_st.vehicle = 1;
    }
    else if( event_input_data_get( EVENT_VEHICLE ) == 2 )
    {
        sys_run_st.vehicle = 2;
    }
    //3、设置系统运行的状态 bit3~bit5：0 - 初始化 1- 系统启动 2 - 正常运行 3 - 睡眠状态 4 - 省电状态 5 - 唤醒状态*/
    sys_run_st.work = sys_work_mode_get();
    if( sys_work_status == SYS_ST_INIT )
    {
        sys_run_st.work = 0;
    }
    else if( sys_work_status == SYS_ST_START )  ///< 系统启动
    {
        sys_run_st.work = 1;
    }
    else if( sys_work_status == SYS_ST_RUN )    ///< 正常运行  
    {
        sys_run_st.work = 2;
    }
    else if( sys_work_status == SYS_ST_SLEEP )  ///< 睡眠状态
    {
        sys_run_st.work = 3;
    }
    else if( sys_work_status == SYS_ST_STOP )   ///< 省电状态
    {
        sys_run_st.work = 4;
    }
    else if( sys_work_status == SYS_ST_WAKE )   ///< 唤醒状态
    {
        sys_run_st.work = 5;
    }    
    //4、设置KL15工作状态，bit6~bit8：0- 初始化 1 - KL15低 2- KL15高*/
    sys_run_st.kl15 = sys_switch_state_get( SYS_KL15 );
    //5、设置车辆状态状态，bit9~bit11: 0- 初始化 1 - 开始 2 - 停止*/
    sys_run_st.veh = sys_switch_state_get( SYS_WVEH );
    //6、设置CAN总线唤醒状态， bit12~bit14: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.can = sys_switch_state_get( SYS_WCAN );
    //7、设置RTC唤醒状态， bit15~bit17: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.rtc = sys_switch_state_get( SYS_WRTC );
    /*8、设置网络唤醒状态， bit18~bit20: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.net = sys_switch_state_get( SYS_WNET );
    /*9、设置BLE唤醒状态， bit21~bit23: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.ble = sys_switch_state_get( SYS_WBLE );
    /*10、设置三轴唤醒状态， bit24~bit26: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.gsen = sys_switch_state_get( SYS_WGSEN );
    /*11、设置定时器唤醒状态， bit27~bit29: 0 - 初始化 1 - 唤醒 2 - 运行 3 - 睡眠 4 - 睡眠中*/
    sys_run_st.time = sys_switch_state_get( SYS_WTIME );
    
    event_input_data_set( EVENT_SYSTEMSTATUS , sys_run_st.status );
}

/** ****************************************************************************
* @remarks       void even_check_wake_up_state( uint32_t res )
* @brief         检测系统唤醒的模式
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_check_wake_up_state( uint32_t res )
{   
    #define  WAKEUP_OK ( 1 )
    uint32_t wakeup_st = 0;
    if( res == SYS_WAKEUP_TRIG_RTC )
    {
        sys_switch_state_set( SYS_WRTC ,SYS_WRTC_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_RTC********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_KL15 )
    {
        #if 0
        if( port_collect_acc_pin_state_get() == 1 )
        {
            sys_switch_state_set( SYS_KL15 ,SYS_KL15_LOW ); 
        }
        else
        {
            sys_switch_state_set( SYS_KL15 ,SYS_KL15_HIGH );
        }
        #endif
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_KL15********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_BLE )
    {
        sys_switch_state_set( SYS_WBLE ,SYS_WBLE_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_BLE********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_CAN )
    {
        sys_switch_state_set( SYS_WCAN ,SYS_WCAN_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_CAN********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_GSEN )
    {
        sys_switch_state_set( SYS_WGSEN ,SYS_WGSEN_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_GSEN********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_NET )
    {
        sys_switch_state_set( SYS_WNET ,SYS_WNET_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_NET********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else if( res == SYS_WAKEUP_TRIG_TIME )
    {
        sys_switch_state_set( SYS_WTIME ,SYS_WTIME_WAKE );
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"********************SYS_WAKEUP_TRIG_TIME********************\r\n",0);
        wakeup_st = WAKEUP_OK;
    }
    else
    {
        log_debug_send(INFOR_MODE,EVENT_MAN_NAME ,"System wake up source abnormal\r\n",0);
    }
    
    if( wakeup_st == WAKEUP_OK )
    {
        power_man_semaphore_put();
        event_system_run_status_time = 0;   /*系统被唤醒后重新设置系统运行寄存器为0，重新计时*/
    }

}


/** ****************************************************************************
* @remarks       static void even_vehicle_stata_warning_detection( void )
* @brief         车辆状态警报检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_vehicle_stata_warning_detection(void)
{
    /*
    发动机熄火，速度为0（车速和GPS速度都为0，三轴静止），acc开，10分钟，自动关ACC（一键启动关ACC+报警，非一键启动只报警；
    （三轴故障不熄火，只报警）

    车辆未熄火，速度为0，停车超过30分钟（不熄火，只通知）
    */
   #define  ENG_START_NO_SPEED_WARNING_TIME     (30 * 60 * GAOZ_TICK_PERIOD_S )
   #define  ENG_STOP_ACC_ON_WARNING_TIME        (10 * 60 * GAOZ_TICK_PERIOD_S )
   static uint32_t eng_start_no_speed_time = 0;
   static uint32_t eng_stop_acc_on_time = 0;
   static uint32_t gsen_check_car_state_time = 0;
   uint32_t wait_time  = 0;
   n808_vehicle_data_t data_vehicle;
   gnss_info_struct gnss_info;
   gsen_bmi160_info_t gsen_data;
   uint8_t sys_gsen = 0;
   uint32_t gsen_abs_value = 0,gsen_abs_x,gsen_abs_y,gsen_abs_z;
   int gsen_abs_value_c;
   float gsen_new_old_value_p;
   static uint32_t gsen_abs_value_old = 0;
   static gsen_check_car_state = 0;


    gsen_lis3dh_data_get(&gsen_data); 
   vehicle_data_now_get(&data_vehicle);
   gaoz_gnss_info_now_get( &gnss_info );
   sys_gsen = sys_hard_state_get(SYS_GSEN);

   
     
    gsen_abs_x = gsen_data.accel.x * gsen_data.accel.x;
    gsen_abs_y = gsen_data.accel.y * gsen_data.accel.y;
    gsen_abs_z = gsen_data.accel.z * gsen_data.accel.z;

    gsen_abs_value = gsen_abs_x + gsen_abs_y + gsen_abs_z;

    gsen_abs_value = sqrt(gsen_abs_value);

    
    if(gsen_abs_value_old == 0)
    {
        gsen_abs_value_old = gsen_abs_value;
    }

    if(gsen_abs_value > gsen_abs_value_old)
    {
        gsen_abs_value_c = gsen_abs_value - gsen_abs_value_old;
    } 
    else 
    {
        gsen_abs_value_c =  gsen_abs_value_old - gsen_abs_value;  
    }

    gsen_new_old_value_p = (float)gsen_abs_value_c/(float)gsen_abs_value_old;

    if(gsen_new_old_value_p > 0.05)
    {
        gsen_check_car_state_time = gaoz_get_systick();
        gsen_check_car_state = 1;
    }
    else 
    {
        if( pdPASS == sys_timestamp_delay(( uint32_t * )&gsen_check_car_state_time, 30))
        { 
            gsen_check_car_state = 0;
        }
    }

 

    gsen_abs_value_old = gsen_abs_value;


   if((data_vehicle.Ignition_status&0x02) == 0x02)//发动机启动
   {
        sys_hard_state_set( SYS_VEHICLE_ACC , SYS_VEHCILE_STATE_NORMAL ); /*解除车辆停止未动报警*/ 
            
        eng_stop_acc_on_time = gaoz_get_systick();

        if((data_vehicle.speed == 0x00) && (gnss_info.gnss_position_info.speed == 0x00))//车速为0
        {
             wait_time = gaoz_get_systick(); 
             wait_time = wait_time - eng_start_no_speed_time;
              
             if( pdPASS == sys_timestamp_delay(( uint32_t * )&eng_start_no_speed_time, ENG_START_NO_SPEED_WARNING_TIME))
             {
                  //车辆未熄火，速度为0，停车超过30分钟（不熄火，只通知）
                 sys_hard_state_set( SYS_VEHICLE_READY , SYS_VEHICLE_STATE_ENG_START_NOT_RUN );  
                 
             }
        }
        else
        {
            sys_hard_state_set( SYS_VEHICLE_READY , SYS_VEHCILE_STATE_NORMAL ); /*解除车辆停止未动报警*/ 
            
           
            eng_start_no_speed_time = gaoz_get_systick(); 
        }
   }
   else
   {
        sys_hard_state_set( SYS_VEHICLE_READY , SYS_VEHCILE_STATE_NORMAL ); /*解除车辆停止未动报警*/ 
            
        eng_start_no_speed_time = gaoz_get_systick(); 

        if(((data_vehicle.Ignition_status&0x08) == 0x08))//ACC打开  gsensor 正常
        { 
            if((data_vehicle.speed == 0x00) && (gnss_info.gnss_position_info.speed == 0x00)&&(gsen_check_car_state == 0x00))//车速为0 GPS速度为0  传感器检测为停止态
            { 
                wait_time = gaoz_get_systick(); 
                wait_time = wait_time - eng_stop_acc_on_time;
                //  gaoz_log_debug_printf(INFOR_MODE,"800b","debug event eng_stop_acc_on  speed == 0 wait time = %d/%d SYS_VEHICLE_ACC=%d\r\n",wait_time,ENG_STOP_ACC_ON_WARNING_TIME,sys_hard_state_get(SYS_VEHICLE_ACC));
                if( pdPASS == sys_timestamp_delay(( uint32_t * )&eng_stop_acc_on_time, ENG_STOP_ACC_ON_WARNING_TIME))
                {
                    // gaoz_log_debug_printf(INFOR_MODE,"800b","debug event eng_stop_acc_on > 10min \r\n");
                       //发动机熄火，速度为0（车速和GPS速度都为0，三轴静止），acc开，10分钟，自动关ACC（一键启动关ACC+报警，非一键启动只报警； 
                    sys_hard_state_set( SYS_VEHICLE_ACC , SYS_VEHICLE_STATE_ACC_NO_OFF );  

                    if((sys_gsen == SYS_GSEN_NORMAL ))//三轴故障 不熄火
                    {
                        vehicle_engine_start_st_set(0,0,2);
                        vehiclect_runing_state_set(CARCTL_RUNING_STARTPOWER); 
                        // gaoz_log_debug_printf(INFOR_MODE,"800b","debug event ctrl mcu [10A6] acc off \r\n"); 
                    } 
                    else 
                    {
                        // gaoz_log_debug_printf(INFOR_MODE,"800b","debug event sys_gsen != SYS_GSEN_NORMAL \r\n");
                    }
                }
            }
            else
            {
                //  gaoz_log_debug_printf(INFOR_MODE,"800b","debug event eng_stop_acc_on  data_vehicle.speed=%x sys_gsen=%x\r\n",data_vehicle.speed,sys_gsen);
                eng_stop_acc_on_time = gaoz_get_systick(); 
            }
        } 
        else
        {
            sys_hard_state_set( SYS_VEHICLE_ACC , SYS_VEHCILE_STATE_NORMAL ); /*解除ACC10分钟未关闭报警*/ 
            // gaoz_log_debug_printf(INFOR_MODE,"800b","debug event eng_stop_acc_off  data_vehicle.Ignition_status=%x gnss_info.gnss_position_info.speed =%x\r\n",data_vehicle.Ignition_status,gnss_info.gnss_position_info.speed );
            eng_stop_acc_on_time = gaoz_get_systick(); 
        }
   }  
}


/******************************************************************************
* @remarks       static void even_lbs_positioning_detection( void )
* @brief         LBS定位状态检测
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void even_lbs_positioning_detection( void )
{
    #define SYS_TIMESTAMP_LBS_TIME_PERIOD   ( 60 * GAOZ_TICK_PERIOD_S )  /*时间戳存储的周期*/
    #define LBS_POSITION_ONCE_TIME  ( 1 )
    #define LBS_POSITION_NOW_TIME   ( 2 )
    static uint32_t lbs_time_counter = 0;
    static uint8_t  lbs_kl15_high = 0,lbs_kl15_low = 0, lbs_valid = 0 ;
    static uint8_t  car_speed_flag = 0;

    if( gnss_status_get() != GPS_MODEL_POSI_OK )            /*定位模块定位数据异常*/
    {                   
        if(power_on_position_flag == 0)//上电一直未定位成功
        {
            if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (SYS_TIMESTAMP_LBS_TIME_PERIOD * 2) ))
            {
                 log_debug_send(DEBUG_MODE,LBS_MAN_NAME ,"even_lbs power_on_position_flag == 0\r\n",0);
                gnss_lbs_wifiscan_position_read();//上电没有定位 则获取一次         
                power_on_position_flag = 1;   
            }
        }
        if( (event_input_data_get( EVENT_SPEED ) > EVENT_NET_ANOMALY_VEH_SPEED) && (sys_switch_state_get( SYS_KL15 ) == SYS_KL15_HIGH))       
        {
            // log_debug_send(DEBUG_MODE,LBS_MAN_NAME ,"even_lbs  && (sys_switch_state_get( SYS_KL15 ) == SYS_KL15_HIGH)\r\n",0);
                 car_speed_flag  = 1;
            if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (SYS_TIMESTAMP_LBS_TIME_PERIOD * 1) ))
            {
                log_debug_send(DEBUG_MODE,LBS_MAN_NAME ,"even_lbs event_input_data_get( EVENT_SPEED ) > EVENT_NET_ANOMALY_VEH_SPEED > 10km/h 1min\r\n",0);
                gnss_lbs_wifiscan_position_read();
                power_on_position_flag  = 1;
                car_speed_flag = 0;
            }
        }
        else 
        {
            //  log_debug_printf(DEBUG_MODE,LBS_MAN_NAME ,"event lbs get speed  =  %d  SYS_KL15 = %d ",event_input_data_get( EVENT_SPEED ),sys_switch_state_get( SYS_KL15 ));    
        }
        if( sys_switch_state_get( SYS_KL15 ) == SYS_KL15_LOW && car_speed_flag)
        {
             log_debug_send(DEBUG_MODE,LBS_MAN_NAME ,"even_lbs sys_switch_state_get( SYS_KL15 ) == SYS_KL15_LOW\r\n",0);
            gnss_lbs_wifiscan_position_read();  
            car_speed_flag = 0;      
        }
        #if 0
        else                                                /*车辆运行速度0或者小于10KM/H*/
        {
            if( sys_switch_state_get( SYS_KL15 ) == SYS_KL15_HIGH )
            {
                lbs_kl15_low = 0;
                switch( lbs_kl15_high )
                {
                    case 0:
                        lbs_time_counter = gaoz_get_systick(); 
                        lbs_kl15_high = 1;
                    break;
                    case 1:
                        if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (SYS_TIMESTAMP_LBS_TIME_PERIOD * 3) ))
                        {
                            lbs_kl15_high = 2;
                        }
                    break;
                    case 2:
                        gnss_lbs_wifiscan_position_read(); 
                        lbs_kl15_high = 3;
                    break;
                    case 3:
                        if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (GAOZ_TICK_PERIOD_S * 10) ))
                        {
                            lbs_kl15_high = 4;
                        }
                        if( gnss_lbs_position_once_state_read() != 0x00 )
                        {
                            lbs_kl15_high = 4;
                        }
                    break;
                    default:
                        lbs_valid = 1;
                    break;
                }
            }
            else if( sys_switch_state_get( SYS_KL15 ) == SYS_KL15_LOW )
            {
                lbs_kl15_high = 0;
                if( lbs_valid == 1 )
                {
                    switch( lbs_kl15_low )
                    {
                        case 0:
                            lbs_time_counter = gaoz_get_systick();
                            lbs_kl15_low = 1;
                        break;
                        case 1:
                            if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (GAOZ_TICK_PERIOD_S * 10) ))
                            {
                                lbs_kl15_low = 2;
                            }
                        break;
                        case 2:
                            gnss_lbs_wifiscan_position_read(); 
                            lbs_kl15_low = 3;
                        break;
                        case 3:
                            if( pdPASS == sys_timestamp_delay(( uint32_t * )&lbs_time_counter, (GAOZ_TICK_PERIOD_S * 10) ))
                            {
                                lbs_kl15_low = 4;
                            }
                            if( gnss_lbs_position_once_state_read() != 0x00 )
                            {
                                lbs_kl15_low = 4;
                            }
                        break;
                        default:
                            break;
                    }
                }
            }
        }
        #endif
    }
    else
    {
        log_debug_send(DEBUG_MODE,LBS_MAN_NAME ,"if( gnss_status_get() == GPS_MODEL_POSI_OK )\r\n",0);
        if(power_on_position_flag == 0)
        {
            log_debug_send(DEBUG_MODE,LBS_MAN_NAME ," power_on_position_flag == 1\r\n",0);
        }
        power_on_position_flag = 1;    
        lbs_kl15_high = 0;
        lbs_kl15_low = 0;
        lbs_time_counter = gaoz_get_systick(); 
    }
}


/** ****************************************************************************
* @remarks       static void event_hand_collect_task( void * pvParameters)
* @brief         事件管理任务
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void event_hand_collect_task( void * pvParameters)
{
    power_on_time = 0;
    event_trig_msg_s event_msg;
    mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )EVENT_MAN_NAME , TASK_SEM_YES ); /*注册任务监控*/
    log_debug_send(DEBUG_MODE,EVENT_MAN_NAME ,"Event management task started successfully\r\n",0);
    for( ;; )
    {
        power_on_time++;

        mon_reset_task_dog();

        even_kl15_state_detection();          /*检测KL15的状态*/
        
        even_power_state_detection();         /* 检测电源状态 */
        
        even_battery_state_detection();       /* 备用电池检测*/
        
        even_rtc_state_detection();           /* 检测系统RTC状态*/
        
        even_gnss_state_detection();          /* 检测GNSS状态 */
        
        even_net_state_detection();           /* 检测NET状态 */

        even_sim_state_detection();           /* 检测SIM卡状态 */
        
        even_gsen_state_detection();          /* 检测GSEN状态 */
        
        even_can_state_detection();           /* 检测CAN/MCU状态*/
        
        even_flash_state_detection();         /* 检测FLASH状态*/
        
        even_ble_state_detection();           /* 检测BLE状态*/

        even_net_anomaly_detection();         /* 检测网络异常状态 */

        even_vehicle_shifting_detection();    /* 检测车身运动状态，GPS运行距离*/
        
        even_net_data_volume_detection();     /* 检测网络数据发送、接收总量*/

        even_gnss_sensor_speed_data_detection(); /*检测车辆速度，灌入速度值保证高精度定位的准确性*/
        
        even_ble_connect_time_detection();    /* 检测BLE连接的时间*/

        even_system_run_status_detection();   /* 检测系统运行的状态*/

        even_lbs_positioning_detection();     /*检测LBS定位状态*/

        even_vehicle_stata_warning_detection();         /*检测车辆状态*/

        if( event_queue_get( &event_msg ) == 1 )
        {
            switch( event_msg.command_id )
            {   
                case EVENT_TRIG_PWR:                  /* 系统停止时进入快速事件扫描*/
                    power_man_semaphore_put();
                break;
                case EVENT_TRIG_RTC:                  /* RTC周期性事件采集 处理周期1S*/
                    
                break;
                case EVENT_TRIG_INT:                  /* 外部事件中的唤醒*/
                    even_check_wake_up_state(event_msg.returns_res);
                break;
                case EVENT_TRIG_OTA:                  /* 远程OTA事件升级条件处理*/
                    
                break;
                case EVENT_TRIG_CAN:                  /* CAN控制事件处理*/
                    
                break;
            }
        }
    }
}

